<script>

export default {
  name: 'UserinfoEdit',
  data () {
    return {
      show: false,
      formData: {
        userinfoId: '',
        username: '',
        nickname: '',
        phone: '',
        profile: '',
        href: '',
        avatar: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名' }],
        phone: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1\d{10}$/, message: '手机号格式错误' }
        ],
        nickname: [{ required: true, message: '请输入昵称' }]
      },
      uploadUrl: '',
      headers: {},
      avatarUrl: '',
      basePath: '',
      url: ''
    }
  },
  methods: {
    init (id) {
      this.show = true
      this.formData = { ...this.$options.data().formData }
      this.avatarUrl = ''
      if (id) {
        this.$axios.get(`userinfo/info/${id}`).then(data => {
          this.formData = { ...data }
          if (data.avatar.startsWith('http')) {
            this.avatarUrl = data.avatar
          } else {
            this.avatarUrl = this.basePath + data.avatar
          }
        })
      }
    },
    beforeUpload (file) {
      const isImage = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt20M = file.size / 1024 / 1024 < 20

      if (!isImage) {
        this.$message.error('上传图片只能是 JPG或PNG 格式!')
      }
      if (!isLt20M) {
        this.$message.error('上传图片大小不能超过 20MB!')
      }
      return isImage && isLt20M
    },
    uploadSuccess (res) {
      if (res.code === 200) {
        this.avatarUrl = this.basePath + res.data.filename
        this.formData.avatar = res.data.filename
      } else {
        this.$message.error(res.msg)
      }
    },
    submit () {
      this.$refs.userinfoForm.validate(valid => {
        if (valid) {
          if (!this.formData.href) {
            this.formData.href = this.formData.username
          }
          this.$axios.post('userinfo/update', this.formData).then(() => {
            this.$message.success('操作成功！')
            this.show = false
            this.$emit('close')
          }).catch(e => e)
        }
      })
    }
  },
  created () {
    this.basePath = process.env.VUE_APP_BASE_PATH
    this.uploadUrl = this.basePath + 'upload'
    this.headers = {
      token: this.$cookies.get('token')
    }
    this.url = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port
  }
}
</script>

<template>
<el-dialog title="用户信息" :visible.sync="show" v-if="show">
  <el-form ref="userinfoForm" :model="formData" :rules="rules" label-width="80px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="formData.username" :disabled="formData.userinfoId !== ''" placeholder="请输入用户名"/>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="formData.phone" :disabled="formData.userinfoId !== ''" placeholder="请输入手机号"/>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="formData.nickname" placeholder="请输入昵称"/>
    </el-form-item>
    <el-form-item label="个性域名">
      <el-input v-model="formData.href" :placeholder="formData.username">
        <template slot="prepend">{{url}}</template>
      </el-input>
    </el-form-item>
    <el-form-item label="个人简介">
      <el-input v-model="formData.profile" placeholder="请输入个人简介" type="textarea" maxlength="120" show-word-limit/>
    </el-form-item>
    <el-form-item label="头像">
      <el-upload
        class="avatar-uploader"
        :action="uploadUrl"
        :headers="headers"
        :data="{type:'avatar'}"
        :show-file-list="false"
        :on-success="uploadSuccess"
        :before-upload="beforeUpload">
        <img v-if="avatarUrl" :src="avatarUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</el-dialog>
</template>

<style scoped lang="scss">

</style>
